//
//  YYStatusRetweetView.swift
//  YYWeibo
//
//  Created by strongwallyi on 16/1/4.
//  Copyright © 2016年 strongwallyi. All rights reserved.
//

import UIKit
import SnapKit

class YYStatusRetweetView: UIView {
    
    //记录当前控件的底部约束
    var bottomConstraint: Constraint?
    
    //当前转发微博的显示的 model
    var statusViewModel: YYStatusViewModel? {
        didSet{
            contentLabel.text = statusViewModel?.retweetStatusText
            
            //卸载掉之前的约束
            bottomConstraint?.uninstall()
            //根据转发微博中是否有图片来显示和隐藏配图控件
            if let picturls = statusViewModel?.status?.retweeted_status?.pic_urls where picturls.count > 0{
                //有图片
                statusPictureView.hidden = false
                
                //设置要显示的数据
                statusPictureView.picUrls = picturls
                //更改转发微博的约束
                self.snp_updateConstraints { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(statusPictureView.snp_bottom).offset(YYStatusCellMargin).constraint
                }
            }else{
                statusPictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(YYStatusCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor(white: 0.97, alpha: 1)
        
        addSubview(contentLabel)
        addSubview(statusPictureView)
        
        //添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(YYStatusCellMargin)
//            make.trailing.equalTo(self).offset(YYStatusCellMargin)
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -YYStatusCellMargin)
            make.top.equalTo(self).offset(YYStatusCellMargin)
        }
        
        //添加配图的约束
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(YYStatusCellMargin)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
            self.snp_makeConstraints { (make) -> Void in
                self.bottomConstraint = make.bottom.equalTo(statusPictureView).offset(YYStatusCellMargin).constraint
        
        }
    }

    //懒加载转发微博的内容控件
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        //多行
        label.numberOfLines = 0
        return label
    }()
    
    //懒加载配图视图
    private lazy var statusPictureView: YYStatusPictureView = {
        let pictureView = YYStatusPictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()
    
}
